from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.template import RequestContext
from django.contrib import messages
from login.models import *
import requests
import os
#注册页面
flag = False
def register(request):
    return render(request, 'register.html')
#登录页面
def login(request):
    return render(request, 'login.html')
#忘记密码页面
def forget(request):
    return render(request, 'forget.html')
#商家注册页面
def saleregister(request):
    return render(request, 'saleregister.html')
#执行注册页面
@csrf_exempt
def doregister(request):
    data = request.POST
    if data['pwd'] == data['vpwd']:
        a=stu_register.objects.get(pno=data['pno'])
        b=stu_register.objects.get(sno=data['sno'])
        c = sale_register.objects.get(spno=data['pno'])
        if a or c:
            messages.success(request, '该手机号已被注册')
            return render(request, 'register.html')
        elif b:
            messages.success(request, '该学号已经被注册')
            return render(request, 'register.html')
        else:
            obj=stu_register()
            obj.rn=data['rn']
            obj.sno=data['sno']
            obj.pno=data['pno']
            obj.pwd=data['pwd']
            obj.save()
            return render(request, 'login.html')
    else:
        messages.success(request,'两次密码需输入一致')
        return render(request, 'register.html')
@csrf_exempt
def dosregister(request):
    data = request.POST
    if data['pwd'] == data['vpwd']:
        c=sale_register.objects.filter(spno=data['spno'])
        d=sale_register.objects.filter(account=data['account'])
        e = stu_register.objects.filter(pno=data['spno'])
        if c or e:
            messages.success(request, '该手机号已被注册')
            return render(request, 'saleregister.html')
        elif d:
            messages.success(request, '该店名已经被注册')
            return render(request, 'saleregister.html')
        else:
            obj=sale_register()
            obj.account=data['account']
            obj.spno=data['spno']
            obj.pwd=data['pwd']
            obj.save()
            return render(request, 'login.html')
    else:
        messages.success(request,'两次密码需输入一致')
        return render(request, 'saleregister.html')
@csrf_exempt
def dologin(request):
    global flag
    data=request.POST
    x = sale_register.objects.filter(spno=data['account'])
    y = stu_register.objects.filter(pno=data['account'])
    z = stu_register.objects.filter(sno=data['account'])
    if x:
        vpwd=sale_register.objects.get(spno=data['account'])
        if vpwd.pwd==data['pwd']:
            messages.success(request, '登陆成功')
            request.session['username'] =data['account']
            request.session.set_expiry(0)
            username=request.session['username']
            return render(request,'main_login.html', locals())
        else:
            messages.success(request, '密码错误')
            return render(request, 'login.html')
    elif y:
        vpwd = stu_register.objects.get(pno=data['account'])
        if vpwd.pwd == data['pwd']:
            messages.success(request, '登陆成功')
            request.session['username'] = data['account']
            request.session.set_expiry(0)
            return render(request, 'main_login.html', {'username': request.session['username']})
        else:
            messages.success(request, '密码错误')
            return render(request, 'login.html')
    elif z:
        vpwd = stu_register.objects.get(sno=data['account'])
        if vpwd.pwd == data['pwd']:
            messages.success(request, '登陆成功')
            request.session['username'] = data['account']
            request.session.set_expiry(0)
            return render(request, 'main_login.html', {'username': request.session['username']})
        else:
            messages.success(request, '密码错误')
            return render(request, 'login.html')
    else:
        messages.success(request, '账号不存在')
        return render(request, 'login.html')
#商城主页面
def main(request):
    return render(request, 'main.html')

#后台商品，执行增加
def insert(request):
    pass
#后台商品，执行删除
def delete(request):
    pass
#后台商品，执行编辑
def edit(request):
    pass
# Create your views here.
#图片输入数据库测试
def UpLoadInfo(request):
    if request.method == 'POST':
        image = product_info(
            product_pic=request.FILES.get('photo'),
            product_name=request.FILES.get('photo').name
        )
        image.save()
        return render(request, 'upload.html', locals())
    else:
        return render(request, 'upload.html', locals())

#搜索操作
def search(request):
    return render(request,'search.html',locals())

@csrf_exempt
def dosearch(request):
    sale=request.session.get('sale_login', '')
    stu=request.session.get('stu_login', '')
    if sale or stu:
        data = request.POST
        x=product_info.objects.filter(product_name__contains=data['keyword'])
        real=x.values()
        x=len(real)
        product_pic={}
        for i in range(0,x):
            product_pic[i]=os.path.join('/static/media/',real[i]['product_pic'])
        return render(request, 'search.html',{'username':request.session['username']} )
    else:
        return render(request, 'login.html',)
def main_login(request):
    return render(request, 'main_login.html', {'username': request.session['username']})
def personal(request):
    username = request.session['username']
    return render(request,'personal.html',locals())